from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class HomePage:
    def __init__(self, driver):
        self.driver = driver
        self.url = 'http://your-ecommerce-app/'
        self.search_input = (By.ID, 'search-input')
        self.search_button = (By.ID, 'search-button')
        self.category_links = (By.CSS_SELECTOR, 'ul.categories li a')
        self.promotion_banners = (By.CSS_SELECTOR, 'div.promotion-banners div.banner')
        self.recommended_products = (By.CSS_SELECTOR, 'div.recommended-products .product-item')
        self.header_logo = (By.ID, 'header-logo')
        self.footer_copyright = (By.ID, 'footer-copyright')

    # 导航至首页
    def navigate(self):
        self.driver.get(self.url)

    # 在首页上搜索指定的商品名称
    def search_product(self, product_name):
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.search_input)
        ).send_keys(product_name)
        WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.search_button)
        ).click()

    # 验证搜索结果页面是否包含指定的商品名称
    def verify_search_results(self, product_name):
        search_results = (By.XPATH, f"//h2[contains(text(), '{product_name}')]")
        return WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(search_results)
        )

    # 点击首页上的类别链接
    def click_category_link(self, category_name):
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.category_links)
        )
        for link in self.driver.find_elements(*self.category_links):
            if link.text == category_name:
                link.click()
                break

    # 验证类别页面是否包含指定的类别名称
    def verify_category_page(self, category_name):
        category_title = (By.XPATH, f"//h1[contains(text(), '{category_name}')]")
        return WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(category_title)
        )

    # 验证首页上的促销横幅是否可见
    def verify_promotion_banners(self):
        return WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.promotion_banners)
        )

    # 验证首页上的推荐商品是否可见
    def verify_recommended_products(self):
        return WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.recommended_products)
        )

    # 验证首页上的头部 Logo 是否可见
    def verify_header_logo(self):
        return WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.header_logo)
        )

    # 验证首页上的底部版权信息是否可见
    def verify_footer_copyright(self):
        return WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.footer_copyright)
        )